/**
* Copyright 2013 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.server.database;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class MySqlStatementList implements Iterable<String> {

	private static final String		STATEMENT_DELIMITER = ";";
	private static final String		DELIMITER_TAG = "DELIMITER ";

	private final ArrayList<String>	statements = new ArrayList<String>();
	private String						delimiter;

	public MySqlStatementList(final String delimiter) {
		this.delimiter = delimiter;
	}

	public MySqlStatementList() {
		this(STATEMENT_DELIMITER);
	}

	public boolean addAll(final File[] sqlFiles) throws IOException {
		int sz = statements.size();
		for (File sqlFile : sqlFiles)
			add(sqlFile);
		return statements.size() > sz;
	}

	public boolean add(final File sqlFile) throws IOException {
		int sz = statements.size();
		final FileReader reader = new FileReader(sqlFile);
		try {
			final BufferedReader in = new BufferedReader(reader);
			try {
				String statement = "";
				String line;
				// read the SQL file line by line
				while ((line = in.readLine()) != null) {
					line = line.replace('\t', ' ').trim();
					if (line.isEmpty())
						continue;
					// ignore comments beginning with #
					int indexOfCommentSign = line.indexOf('#');
					if (indexOfCommentSign != -1) {
						if (indexOfCommentSign == 0)
							continue;
						line = line.substring(0, indexOfCommentSign);
					}
					// ignore comments beginning with --
					indexOfCommentSign = line.indexOf("--");
					if (indexOfCommentSign != -1) {
						if (indexOfCommentSign == 0)
							continue;
						line = line.substring(0, indexOfCommentSign);
					}
					// ignore comments surrounded by /* */
					indexOfCommentSign = line.indexOf("/*");
					if (indexOfCommentSign != -1) {
						if (indexOfCommentSign == 0)
							line = "";
						else
							line = line.substring(0, indexOfCommentSign);
						String comments;
						// ignore all characters within the comment
						do {
							comments = in.readLine();
						} while (comments != null && !comments.contains("*/"));
						if (comments != null && !comments.endsWith("*/"))
							line += " " + line.substring(indexOfCommentSign+2);
						line = line.trim();
						if (line.isEmpty())
							continue;
					}
					if (line.startsWith(DELIMITER_TAG)) {
						delimiter = line.substring(DELIMITER_TAG.length()).trim();
						continue;
					}
					int indexOfDelimiter = line.indexOf(delimiter);
					if (indexOfDelimiter != -1) {
						if (indexOfDelimiter > 0)
							statement += " " + line.substring(0, indexOfDelimiter);
						final String tmp = statement.trim();
						if (!tmp.isEmpty())
							statements.add(tmp);
						if (line.endsWith(delimiter))
							statement = "";
						else
							statement = line.substring(indexOfDelimiter + delimiter.length());
					} else
						statement += " " + line;
				}
			} finally {
				in.close();
			}
		} finally {
			reader.close();
		}
		return statements.size() > sz;
	}

	@Override
	public Iterator<String> iterator() {
		return statements.iterator();
	}

	public boolean isEmpty() {
		return statements.isEmpty();
	}
}